package com.qiyu.admin.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiyu.admin.system.model.Function;
import com.qiyu.admin.system.service.FunctionService;
import com.qiyu.admin.system.util.ModelUtil;
import com.qiyu.framework.util.QiYuUtil;
import com.qiyu.framework.util.ResponseJson;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * 功能资源控制器
 *
 * @author zhangqing
 * @date 2017年04月07日
 */
@RestController
@RequestMapping(value = "/function")
public class FunctionController {
    @Resource
    private FunctionService functionService;

    @RequestMapping(value = "/main",method = RequestMethod.GET)
    public ModelAndView funMain(){
        ModelAndView modelAndView = new ModelAndView("system/function/function_main");
        return modelAndView;
    }
    @RequestMapping(value = "/add/view",method = RequestMethod.GET)
    public ModelAndView addView(String funId, ModelMap modelMap){
        modelMap.addAttribute("parentId",funId);
        ModelAndView modelAndView = new ModelAndView("system/function/function_add");
        return modelAndView;
    }
    @RequestMapping(value = "",method = RequestMethod.DELETE)
    public ResponseJson deleteFun(Function function) throws Exception{
        function.setActiveFlag(-1);//状态改为-1,代表逻辑删除
        ModelUtil.updateInit(function);
        functionService.updateByPrimaryKeySelective(function);//修改操作
        return QiYuUtil.getResponseJson("删除成功",true,null);
    }
    @RequestMapping(value = "/update/view",method = RequestMethod.GET)
    public ModelAndView functionUpdatView(String functionId,ModelMap modelMap) throws Exception{
        modelMap.addAttribute("funInfo",functionService.selectByPrimaryKey(functionId));
        ModelAndView modelAndView = new ModelAndView("system/function/function_update",modelMap);
        return modelAndView;
    }

    @RequestMapping(value = "/json/list",method = RequestMethod.GET)
    public ResponseJson funJsonData() throws Exception{
        return QiYuUtil.getResponseJson("查询成功",true, JSONArray.toJSON(functionService.getFunctionZtreeData()));
    }

    @RequestMapping(value = "/info/{funId}",method = RequestMethod.GET)
    public ResponseJson getFunInfo(@PathVariable(value = "funId")String funId) throws Exception{
        return QiYuUtil.getResponseJson("查询成功",true, JSONArray.toJSON(functionService.selectByPrimaryKey(funId)));
    }
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResponseJson addFunction(Function function) throws Exception{
        ModelUtil.insertInit(function);
        Function resultFunction = functionService.insertSelective(function);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",resultFunction.getId());
        jsonObject.put("pId",resultFunction.getParentId());
        jsonObject.put("name",resultFunction.getFunName());
        return QiYuUtil.getResponseJson("增加成功",true,jsonObject);
    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    public ResponseJson updateFunction(Function function) throws Exception{
        ModelUtil.updateInit(function);
        Function resultFunction = functionService.updateByPrimaryKeySelective(function);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",resultFunction.getId());
        jsonObject.put("pId",resultFunction.getParentId());
        jsonObject.put("name",resultFunction.getFunName());
        return QiYuUtil.getResponseJson("修改成功",true,jsonObject);
    }
}
